home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / eedsrc24.zip / EEP24SRC.ZIP / IGRAPHPS.C < prev    next >
C/C++ Source or Header  |  1992-07-29  |  13KB  |  406 lines

  1. /*****************************************************************************
  2. *   General routines to    handle the graphics.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                   Ver 0.1, Oct. 1989    *
  5. *                                         *
  6. * Support: PostScript to standard output.                     *
  7. *****************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <time.h>
  13.  
  14. #ifdef __MSDOS__
  15. #include <io.h>
  16. #include <fcntl.h>
  17. #include <bios.h>
  18. #include <dos.h>
  19. #include <conio.h>
  20. #include <sys\types.h>
  21. #else
  22. #include <sys/types.h>
  23. #endif /* __MSDOS__ */
  24.  
  25.  
  26. #include "program.h"
  27. #include "igraph.h"
  28.  
  29. #define PS_PSIZE_X 1024                                         /* In points. */
  30. #define PS_PSIZE_Y 640
  31.  
  32. #define PS_ISIZE_X 10.0                                         /* In inches. */
  33. #define PS_ISIZE_Y 6.25
  34.  
  35. #define PS_NORMAL_FONT_SIZE    12
  36.  
  37. static TextOrientationType TextOrientation = TEXT_ORIENT_HORIZ;
  38. static int TextScale = 1;
  39. static TextHorizJustifyType TextXCenter = TEXT_X_CENTER;
  40. static TextVertJustifyType TextYCenter = TEXT_Y_CENTER;
  41.  
  42. static int CursorX, CursorY;
  43. static int GraphicMode = FALSE;
  44.  
  45. static void IGSetFontSize(int Size);
  46.  
  47. /****************************************************************************
  48. * Routine to reset all the system to starting condition    :            *
  49. ****************************************************************************/
  50. void IGInitGraph(void)
  51. {
  52.     static char *PSHeader[] = {
  53.     "%!PS-Adobe-2.0 EPSF-1.2",
  54.     "%%BoundingBox: 36 36 540 756",    /* 7x10 inches, LL at [0.5, 0.5]. */
  55.     "%%Title:",
  56.     "%%Creator: EED-PS",
  57.     "%%CreationDate:",
  58.     "%%EndComments",
  59.     "%%\n",
  60.     "/line {",
  61.     "    newpath",
  62.     "    moveto",
  63.     "    lineto",
  64.     "    stroke",
  65.     "} def\n",
  66.     "gsave",
  67.     "1 setlinewidth",
  68.     "1 setlinecap",
  69.     "1 setlinejoin",
  70.     "72 72 scale\t\t\t\t%% Talk inches",
  71.     "7.0 10.5 translate -90 rotate\t\t%% 0.5 inch border from all sides",
  72.     NULL
  73.     };
  74.     int i;
  75.     time_t time1970 = time(NULL);
  76.  
  77.  
  78.     for (i = 0; PSHeader[i] != NULL; i++) {
  79.     if (strcmp(PSHeader[i], "%%Title:") == 0)
  80.         printf("%s %s\n", PSHeader[i], EEDataFileName);
  81.     else if (strcmp(PSHeader[i], "%%CreationDate:") == 0)
  82.         printf("%s %s", PSHeader[i], ctime(&time1970));
  83.     else
  84.         printf("%s\n", PSHeader[i]);
  85.     }
  86.  
  87.     printf("%lf %lf scale\t\t%% Move to EED coordinates\n",
  88.             PS_ISIZE_X / PS_PSIZE_X, -PS_ISIZE_Y / PS_PSIZE_Y);
  89.  
  90.     IGSetFontSize(PS_NORMAL_FONT_SIZE);                  /* Select font. */
  91.  
  92.     GraphicMode = TRUE;
  93. }
  94.  
  95. /****************************************************************************
  96. * Routine to close and shutdown    graphic    mode :                    *
  97. ****************************************************************************/
  98. void IGCloseGraph(void)
  99. {
  100.     if (GraphicMode) {
  101.     printf("showpage\n");
  102.     printf("grestore\n");
  103.  
  104.     GraphicMode = FALSE;
  105.     }
  106. }
  107. /****************************************************************************
  108. * Routine to set a font size.                            *
  109. ****************************************************************************/
  110. static void IGSetFontSize(int Size)
  111. {
  112.     printf("/%s findfont [%d 0 0 -%d 0 0] makefont setfont\n",
  113.        EEPSFontName, Size, Size);
  114.     printf("/stringheight %d def\n", (int) (0.65 * PS_NORMAL_FONT_SIZE));
  115. }
  116.  
  117. /****************************************************************************
  118. * Routine to map drawing x coordinate into screen space.            *
  119. ****************************************************************************/
  120. int IGMapX(int x)
  121. {
  122.     return x >> IG_DEFAULT_ZOOM_FACTOR;
  123. }
  124.  
  125. /****************************************************************************
  126. * Routine to map drawing y coordinate into screen space.            *
  127. ****************************************************************************/
  128. int IGMapY(int y)
  129. {
  130.     return y >> IG_DEFAULT_ZOOM_FACTOR;
  131. }
  132.  
  133. /****************************************************************************
  134. * Routine to move to a new position, as in Drawing space.            *
  135. ****************************************************************************/
  136. void IGMoveTo(int x, int y)
  137. {
  138.     CursorX = IGMapX(x);
  139.     CursorY = IGMapY(y);
  140. }
  141.  
  142. /****************************************************************************
  143. * Routine to draw to a new position, as in Drawing space.            *
  144. ****************************************************************************/
  145. void IGLineTo(int x, int y)
  146. {
  147.     int NewX, NewY;
  148.  
  149.     printf("%d %d %d %d line\n\n", CursorX, CursorY,
  150.                     NewX = IGMapX(x), NewY = IGMapY(y));
  151.     CursorX = NewX;
  152.     CursorY = NewY;
  153. }
  154.  
  155. /****************************************************************************
  156. * Routine to draw a line between the two given points.                *
  157. ****************************************************************************/
  158. void IGLine(int x1, int y1, int x2, int y2)
  159. {
  160.     printf("%d %d %d %d line\n\n", IGMapX(x1), IGMapY(y1),
  161.                    IGMapX(x2), IGMapY(y2));
  162. }
  163.  
  164. /****************************************************************************
  165. * Routine to draw a new polyline and fill it if Fill.                *
  166. ****************************************************************************/
  167. void IGPoly(int n, int *Points, int Fill)
  168. {
  169.     int i;
  170.  
  171.     printf("newpath\n");
  172.     printf("%d %d moveto\n", IGMapX(Points[0]), IGMapY(Points[1]));
  173.     for (i = 1; i < n; i++) {
  174.     printf("%d %d lineto\n", IGMapX(Points[i * 2]),
  175.                  IGMapY(Points[i * 2 + 1]));
  176.     }
  177.  
  178.     if (Fill)
  179.     printf("closepath fill\n\n");
  180.     else
  181.     printf("stroke\n\n");
  182. }
  183.  
  184. /****************************************************************************
  185. * Routine to draw a bar, as in Drawing space.                    *
  186. ****************************************************************************/
  187. void IGBar(int x1, int y1, int x2, int y2)
  188. {
  189.     x1 = IGMapX(x1);
  190.     y1 = IGMapY(y1);
  191.     x2 = IGMapX(x2);
  192.     y2 = IGMapY(y2);
  193.     printf("newpath\n");
  194.     printf("%d %d moveto\n", x1, y1);
  195.     printf("%d %d lineto\n", x1, y2);
  196.     printf("%d %d lineto\n", x2, y2);
  197.     printf("%d %d lineto\n", x2, y1);
  198.     printf("%d %d lineto\n", x1, y1);
  199.     printf("closepath fill\n\n");
  200. }
  201.  
  202. /****************************************************************************
  203. * Routine to draw a circle, as in Drawing space.                *
  204. ****************************************************************************/
  205. void IGCircle(int x, int y, int r)
  206. {
  207.     printf("newpath %d %d %d 0 360 arc stroke\n\n", IGMapX(x), IGMapY(y),
  208.                         r >> IG_DEFAULT_ZOOM_FACTOR);
  209. }
  210.  
  211. /****************************************************************************
  212. * Routine to draw an arc, as in Drawing space.                    *
  213. * As the Y axes is inverted the Angles should be inverted as well.        *
  214. ****************************************************************************/
  215. void IGArc(int x, int y, int StAngle, int EndAngle, int r)
  216. {
  217.     printf("newpath %d %d %d %d %d arc stroke\n\n", IGMapX(x), IGMapY(y),
  218.                 r >> IG_DEFAULT_ZOOM_FACTOR, StAngle, EndAngle);
  219. }
  220.  
  221. /****************************************************************************
  222. * Routine to draw the given text in current cursor position.            *
  223. ****************************************************************************/
  224. void IGText(char *s)
  225. {
  226.     printf("\n%d %d moveto (%s)\n", CursorX, CursorY, s);
  227.  
  228.     if (TextScale > 1)
  229.     IGSetFontSize(PS_NORMAL_FONT_SIZE * TextScale);          /* Select font. */
  230.  
  231.     switch (TextXCenter) {
  232.     case TEXT_X_LEFT:
  233.         switch (TextOrientation) {
  234.         case TEXT_ORIENT_HORIZ:
  235.             break;
  236.         case TEXT_ORIENT_VERT:
  237.             printf("stringheight 2 add 0 rmoveto\n");
  238.             break;
  239.         }
  240.         break;
  241.     case TEXT_X_CENTER:
  242.         switch (TextOrientation) {
  243.         case TEXT_ORIENT_HORIZ:
  244.             printf("dup stringwidth pop 2 div neg 0 rmoveto\n");
  245.             break;
  246.         case TEXT_ORIENT_VERT:
  247.             printf("stringheight 2 div 0 rmoveto\n");
  248.             break;
  249.         }
  250.         break;
  251.     case TEXT_X_RIGHT:
  252.         switch (TextOrientation) {
  253.         case TEXT_ORIENT_HORIZ:
  254.             printf("dup stringwidth pop neg -2 add 0 rmoveto\n");
  255.             break;
  256.         case TEXT_ORIENT_VERT:
  257.